home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
EnigmA Amiga Run 1995 October
/
EnigmA AMIGA RUN 01 (1995)(G.R. Edizioni)(IT)[!][issue 1995-10][Aminet 7].iso
/
Aminet
/
util
/
shell
/
NVX_wi1.lha
/
WhereIs.c
< prev
next >
Wrap
C/C++ Source or Header
|
1992-09-02
|
7KB
|
251 lines
;/* WhereIs - Execute me to compile me with Lattice.
LC -b1 -cfistq -v -y WhereIs.c
Blink FROM LIB:c.o,WhereIs.o TO WhereIs LIBRARY LIB:LC.lib,LIB:Amiga.lib NODEBUG
quit
*/
/* ******************************************************************
Program Name: WhereIs YET_ANOTHER_NVX_UTIL
Author: M.Meany ( STIGG // Nerve Axis )
Description: Searches a path for all occurrences of a file that
matches a given pattern. Recursivley enters all
sub-directories of specified search path.
Current Version: 1.00
Known Bugs:
Comments: SET TABS TO 4
Requires dos.library v37 for case insensetive pattern
matching, this means release 2.04 or better!
Revision History: v1.00 Initial Release
********************************************************************* */
/* Searches a specified path for ALL occurences of a given file */
/* Uses DOS pattern matching to check file discriptions */
/* Recursivley enters nested directories as they are encountered */
/* ************************ */
/* Include Header Files */
/* ************************ */
#include <stdio.h>
#include <string.h>
#include <exec/exec.h>
#include <libraries/dos.h>
#include <libraries/dosextens.h>
#include <proto/dos.h>
#include <proto/exec.h>
/* ************************ */
/* Function Prototypes */
/* ************************ */
extern struct DOSBase *DOSBase;
void About( void );
void CheckEntry( char *name, char *dir );
void TackDir( char *dir );
void UntackDir( char *dir );
void AddFile( char *name, char *file );
/* ************************ */
/* Main() */
/* ************************ */
void main( int argc, char *argv[] )
{
int i;
LONG len;
BYTE *tokens;
/* Exit now if called from WorkBench */
if( !argc )
{
About();
Delay( 50*10 );
return;
}
/* We need at least release 2.04 (v37) of dos for this program */
if ( ((struct Library *)DOSBase)->lib_Version < 37 )
{
printf( "Sorry, you need WorkBench 2.04 or later to run me!\n" );
return;
}
/* And at least 3 parameters (this also sorts out a ? parameter ) */
if( argc < 3 )
{
About();
return;
}
/* Determine length of search string */
len = 1 + (LONG)strlen( argv[1] );
if( len > 1 )
{
/* Allocate memory for DOS to build tokens in */
len = 2 +(len<<1);
if( tokens = AllocVec( len, MEMF_PUBLIC|MEMF_CLEAR ) )
{
/* Build tokens */
if( ParsePatternNoCase( argv[1], tokens, len ) >= 0 )
{
/* Now step through each of the specified paths */
for( i=2; i<argc; i++ )
{
/* Tell user what path is being examined */
printf( "\nSearching '1m%s0m'.\n", argv[i] );
/* And search this path */
CheckEntry( tokens, argv[i] );
}
}
else
printf( "Error parsing search string!\n" );
FreeVec( tokens );
}
else
printf( "Error allocating a buffer for tokenised search string!\n" );
}
else
printf( "You must specify a search pattern!\n" );
}
/* ************************ */
/* Show Usage Text */
/* ************************ */
void About(void)
{
printf( " 1mWhereIs, by STIGG // Nerve Axis 1994.0m\n");
printf( "1mUsage:0m WhereIs SearchPattern Path1 [Path2] [Path3]...[PathN]\n");
printf( "1mExamples:0m WhereIs main.c dh0: dh1: dh2\n");
printf( " WhereIs *.asm dh0:\n");
printf( " WhereIs *.doc dh0:SomeUtility\n");
printf( " WhereIs main.? dh0:Code\n");
printf( " WhereIs main* dh0:CSource dh1:Code\n");
printf( " WhereIs *my* dh0: dh1: cd0:\n\n");
printf( "Any valid AmigaDOS pattern will do for this program!\n\n" );
printf( "Now try a friendly 1mNerve Axis0m BBS:\n" );
printf( " 1mTrick Or Treat II0m +44 (0)1703 391797\n");
printf( " 1m13th Hour0m +44 (0)1704 505845\n");
printf( " 1mThe Edge0m +44 (0)1226 289303\n");
printf( " 1mPower Plant0m +44 (0)1229 431590\n");
}
/* ************************ */
/* Checks Files/Enters Dirs */
/* ************************ */
/* Must be called with dir->A directory name, else no joy! */
void CheckEntry( char *name, char *dir )
{
struct FileInfoBlock *fib;
struct FileLock *lock, *oldlock;
/* Add current dir name to global path name */
TackDir( dir );
/* Get a lock on this directory */
if( lock = (struct FileLock *)Lock( dir, ACCESS_READ ) )
{
/* CD into this directory */
oldlock = (struct FileLock *)CurrentDir( (BPTR)lock );
/* Allocate memory for a File Info Block */
if( fib=AllocVec( sizeof( struct FileInfoBlock ), MEMF_CLEAR ) )
{
/* Get details on this directory */
if( Examine( (BPTR)lock, fib ) )
{
/* Now examine each entry in directory */
/* Note that we stay in this loop until all entries have been */
/* checked. If we call ourself, we still come back into this */
/* loop. Thats why a FIB is allocated each call and not set */
/* as a global variable! */
while( ExNext( (BPTR)lock, fib ) )
{
/* If next entry is a file, see if it fits pattern */
if( fib->fib_DirEntryType < 0 )
AddFile( name, fib->fib_FileName );
else
/* If its not a file, it a dir! Enter it (recurse)*/
CheckEntry( name, fib->fib_FileName );
}
}
/* Free File Info Block */
FreeVec( fib );
}
/* CD Back to original directory */
CurrentDir( (BPTR)oldlock );
/* And release the lock */
UnLock( (BPTR)lock );
}
/* Pull last dir name from global path name */
UntackDir( dir );
}
static char dtrack[512]="";
WORD dtpos = 0;
/* ************************ */
/* Add to Global Pathname */
/* ************************ */
void TackDir( char *dir )
{
/* Add dir name to the path */
while( *dir )
{
dtrack[ dtpos ] = *dir++;
dtpos++;
}
/* if last char was a : terminate the path name */
if( dtrack[ dtpos-1 ] == ':' )
dtrack[ dtpos ] = '\0';
/* else tack a / on to it */
else
{
dtrack[ dtpos ] = '/';
dtpos++;
dtrack[ dtpos ] = '\0';
}
}
/* *************************** */
/* Remove from Global Pathname */
/* *************************** */
void UntackDir( char *dir )
{
WORD i;
if( dtpos>2 )
{
/* Find penultimate / or : in path name */
for( i=dtpos-2; !(dtrack[i]=='/') && !(dtrack[i]==':') && i; i-- );
/* Now terminate the string just past delimiter */
if( dtrack[i]=='/' || dtrack[i]==':' ) { dtrack[i+1]='\0'; dtpos=i+1; }
/* If we failed, clear the global path name */
if( !i ) { dtrack[i]='\0'; dtpos=0; }
}
else
{
/* Must be at root already, clear global pathname */
dtpos=0;
dtrack[0] = '\0';
}
}
/* ************************ */
/* Do Pattern Check on File */
/* ************************ */
void AddFile( char *name, char *file )
{
/* If DOS says file fits, display its full path name */
if( MatchPatternNoCase( name, file ) ) printf( "%s%s\n", dtrack, file );
}